#!/bin/sh
echo $(date)
# written by Ole Henrik Jahren <olehenja@stud.ntnu.no>
#
# HOWTO set this script as a cronjob
# 1. Configure the config variables (see below)
# 2. Copy the script to /etc/cron.daily and make sure it is executable.
#    This is dependant on your system installation of cron. Some cronds
#    do not use /etc/cron.{hourly,daily,weekly,monthly} directories, and
#    you will have to manually add them to the system (or a spesific users)
#    crontab.
#
# USER CONFIGURATION VARIABLES
#
# Number of days in advance the system will start pestering members with
# unconfirmed shift notifications
REMIND_DAYS=4

# Username used to log into the MySQL installation
SQL_USER=trkh

# Password for $SQL_USER on the MySQL installation
SQL_PASS=qdw5nef2f7uRqELL

# Database to connect to on the MySQL installation 
SQL_DB=shiftmanagement

# Additional options to pass to mysql(1)
# -N means no column headers, do not remove this one, else weird stuff will
#    ensue.
# --host=$host means you want to connect on a database on $host
# --socket=$sock mean you want to connect to local unix socket $sock
SQL_OPTS="-N --host=localhost"

# The from name that is passed in the envelope to sendmail(1)
MAIL_SENDER_NAME="TRKH Vaktutkalling"

# The from address that is passed int he envelope to sendmail(1)
MAIL_SENDER_ADDR="trkh-utkalling@trondheim-redcross.no"

# Adds the configurable part of the links to the mail. This is typically
# set to the host and an optional directory. E.g. if the application is
# installed in the redcross directory on the host example.com, $BASE_URL
# is set to "example.com/redcross"
BASE_URL="localhost/redcross"

# END OF USER CONFIGURATION VARIABLES

REMIND_SECS=$(echo "$REMIND_DAYS * 24 * 60 * 60" | bc)

get_name()
{
	echo "$*"|cut -f1,2|tr '\t' ' '
}

get_email()
{
	echo "$*"|cut -f3
}

get_roleid()
{
	echo "$*"|cut -f4
}

get_shiftid()
{
	echo "$*"|cut -f5
}

mysql $SQL_OPTS "--user=$SQL_USER" "--pass=$SQL_PASS" "$SQL_DB" << EOF |
SELECT m.first_name, m.last_name, e.address, sr.id, s.id
FROM redcross_shift s, redcross_shift_role sr, redcross_member m, redcross_email e
WHERE s.from_date > NOW()
	AND UNIX_TIMESTAMP(s.from_date) - UNIX_TIMESTAMP() < $REMIND_SECS
	AND s.id = sr.shift_id
	AND sr.member_id = m.id
	AND m.id = e.member_id
	AND e.is_main = 1;
EOF
while read line; do
	name=$(get_name "$line")
	email=$(get_email "$line")
	roleid=$(get_roleid "$line")
	shiftid=$(get_shiftid "$line")
	date=$(LC_ALL=C date "+%a, %e %b %Y %H:%M:%S %z")
	#echo "$name:$email:$roleid:$shiftid"
#Here you can configure the mail that will be sent to users to remind them to confirme shift notifications
	sendmail -F "$MAIL_SENDER_NAME" -f "$MAIL_SENDER_ADDR" "$email" << EOF
Date: $date
From: $MAIL_SENDER_NAME <$MAIL_SENDER_ADDR>
To: $name <$email>
Subject: Har du glemt =?utf-8?Q?=C3=A5_bekreft?=
	=?utf-8?Q?e?= vaktutkallingen for vakt $shiftid?
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline

Hei,

minner deg på at du må godkjenne vaktutkallingen din ved å 1) gå til
http://$BASE_URL/core/shift/confirm/id/$roleid
eller 2) ved å kontakte vaktutkaller.

Du kan se hvilken vakt det er snakk om, på:
http://$BASE_URL/admin/shift/view/id/$shiftid

Vennlig hilsen
Systemet
EOF
done
